package com.tybbank.Utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import android.widget.Toast;

import com.tybbank.R;

import static com.tybbank.Utils.Constant.DEBUG;

/**
 * Created by Administrator on 2018/10/30 0030.
 */
//日志打印
public class logUtilssss {
    private static final String TAG = "LogUtil";
    // Log开关

    // 日志打印
    public static void d(String message) {
        if (DEBUG)
            Log.d(TAG, createMessage(message));
    }

    public static void e(String message) {
        if (DEBUG)
            Log.e(TAG, createMessage(message));
    }

    public static void i(String message) {
        if (DEBUG)
            Log.i(TAG, createMessage(message));
    }

    public static void v(String message) {
        if (DEBUG)
            Log.v(TAG, createMessage(message));
    }

    public static void w(String message) {
        if (DEBUG)
            Log.w(TAG, createMessage(message));
    }

    public static void wtf(String message) {
        if (DEBUG)
            Log.wtf(TAG, createMessage(message));
    }

    public static void println(String message) {
        if (DEBUG)
            Log.println(Log.INFO, TAG, message);
    }

    /**
     * 获取有类名与方法名的logString
     *
     * @param rawMessage
     * @return
     */
    private static String createMessage(String rawMessage) {
        /**
         * Throwable().getStackTrace()获取的是程序运行的堆栈信息，也就是程序运行到此处的流程，以及所有方法的信息
         * 这里我们为什么取2呢？0是代表createMessage方法信息，1是代表上一层方法的信息，这里我们 取它是上两层方法的信息
         */
        StackTraceElement stackTraceElement = new Throwable().getStackTrace()[2];
        String fullClassName = stackTraceElement.getClassName();
        String className = fullClassName.substring(fullClassName
                .lastIndexOf(".") + 1);
        return className + "." + stackTraceElement.getMethodName() + "(): "
                + rawMessage;
    }

    // 吐司
    public static void toast (Activity activity,String  s){
        Toast.makeText(activity,s,Toast.LENGTH_SHORT).show();
    }
    public static void toasteero (Activity activity){
        Toast.makeText(activity,(R.string.error),Toast.LENGTH_SHORT).show();
    }

    //版本号
    public static int getVersionCode(Context context) {
        return getPackageInfo(context).versionCode;
    }

    private static PackageInfo getPackageInfo(Context context) {
        PackageInfo pi = null;

        try {
            PackageManager pm = context.getPackageManager();
            pi = pm.getPackageInfo(context.getPackageName(),
                    PackageManager.GET_CONFIGURATIONS);

            return pi;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return pi;
    }

    //截取电话号码
    public static String substringPhone(String phone) {
        String s = phone.substring(0, 3) + "****" + phone.substring(7, phone.length());
        return s;
    }
}
